﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ProcessUtilities.cs" company="">
//   
// </copyright>
// <summary>
//   The process utilities.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace TinyRadio.UserInterface.Infrastructure
{
    using System;
    using System.Diagnostics;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Threading;
    using System.Windows;

    /// <summary>The process utilities.</summary>
    public static class ProcessUtilities
    {
        #region Constants

        /// <summary>The sw restore.</summary>
        private const int SwRestore = 9;

        #endregion

        #region Static Fields

        /// <summary>The _mutex.</summary>
        private static Mutex _mutex;

        #endregion

        #region Public Methods and Operators

        /// <summary>The set focus to previous instance.</summary>
        /// <param name="processName">The process Name.</param>
        /// Set focus to the previous instance of the specified program.
        public static void SetFocusToPreviousInstance(string processName)
        {
            IntPtr hWnd = IntPtr.Zero;

            foreach (Process process in Process.GetProcesses().Where(process => process.ProcessName.Equals(processName))
                )
            {
                hWnd = process.MainWindowHandle;
            }

            IntPtr hPopupWnd = GetLastActivePopup(hWnd);

            if (IsWindowEnabled(hPopupWnd))
            {
                hWnd = hPopupWnd;
            }

            SetForegroundWindow(hWnd);

            if (IsIconic(hWnd))
            {
                ShowWindow(hWnd, SwRestore);
            }
        }

        /// <summary>The this process is already running.</summary>
        /// Determine if the current process is already running
        /// <returns>The <see cref="bool"/>.</returns>
        public static bool ThisProcessIsAlreadyRunning()
        {
            bool createdNew;
            _mutex = new Mutex(false, Application.Current.MainWindow.GetType().Assembly.FullName, out createdNew);
            return !createdNew;
        }

        #endregion

        #region Methods

        /// <summary>The find window.</summary>
        /// <param name="lpClassName">The lp class name.</param>
        /// <param name="lpWindowName">The lp window name.</param>
        /// <returns>The <see cref="IntPtr"/>.</returns>
        [DllImport("user32.dll", SetLastError = true)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        /// <summary>The get last active popup.</summary>
        /// <param name="hWnd">The h wnd.</param>
        /// <returns>The <see cref="IntPtr"/>.</returns>
        [DllImport("user32.dll")]
        private static extern IntPtr GetLastActivePopup(IntPtr hWnd);

        /// <summary>The is iconic.</summary>
        /// <param name="hWnd">The h wnd.</param>
        /// <returns>The <see cref="bool"/>.</returns>
        [DllImport("user32.dll")]
        private static extern bool IsIconic(IntPtr hWnd);

        /// <summary>The is window enabled.</summary>
        /// <param name="hWnd">The h wnd.</param>
        /// <returns>The <see cref="bool"/>.</returns>
        [DllImport("user32.dll")]
        private static extern bool IsWindowEnabled(IntPtr hWnd);

        /// <summary>The set foreground window.</summary>
        /// <param name="hWnd">The h wnd.</param>
        /// <returns>The <see cref="bool"/>.</returns>
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        /// <summary>The show window.</summary>
        /// <param name="hWnd">The h wnd.</param>
        /// <param name="nCmdShow">The n cmd show.</param>
        /// <returns>The <see cref="bool"/>.</returns>
        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        #endregion
    }
}